package com.company.cloud.web.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.company.cloud.web.service.WebArtService;
import com.company.cloud.web.service.WebIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;

/**
 * 艺术家
 */
@Controller
@RequestMapping("/artist")
public class ArtistController {

    @Autowired
    private WebArtService webArtService;

    @Autowired
    private WebIndexService webIndexService;

    /**
     * 艺术家列表
     */
    @RequestMapping("/list")
    public String list(Model model, @RequestParam Map<String, String> params) {
        System.out.println("++++++++++++ ArtistController:list");
        JSONObject list = webArtService.artistList(params);
        model.addAttribute("data", list);
        Map<String, Object> indexMap = webIndexService.getPageIndexData();
        model.addAttribute("category", indexMap.get("category"));
        model.addAttribute("menu", indexMap.get("menu"));
        model.addAttribute("footer", indexMap.get("footer"));
        return "artist/list";
    }


    /**
     * 艺术家拼音字母列表
     */
    @RequestMapping("/index")
    public String artistListPy(Model model, String nameFirstPy,
                               @RequestParam Map<String, String> params,
                               @CookieValue(value = "org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE", defaultValue = "zh-CN") String i18n) {
        System.out.println("++++++++++++ ArtistController:artistListPy");

        JSONObject list = webArtService.artistListPy(nameFirstPy);
        model.addAttribute("data", list);


        Map<String, Object> indexMap = webIndexService.getPageIndexData();
        model.addAttribute("category", indexMap.get("category"));

        model.addAttribute("exArtistBanner", indexMap.get("exArtistBanner"));
        model.addAttribute("friendLink", indexMap.get("friendLink"));
        model.addAttribute("copyright", indexMap.get("copyright"));
        Map<String, Object> menusMap = webArtService.staticPageTree("top");
        model.addAttribute("menu", menusMap);

        model.addAttribute("i18n", i18n);
        Map<String, Object> footer = webArtService.staticPageTree("bottom");
        model.addAttribute("footer", footer);
        return "artist/index";
    }


    /**
     * 艺术家详细信息
     */
    @RequestMapping("/detail")
    public String detail(Model model, String artistNo, @CookieValue(value = "org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE", defaultValue = "zh-CN") String i18n) {
        System.out.println("++++++++++++ ArtistController:detail");
        JSONObject artist = webArtService.artistInfo(artistNo);
        model.addAttribute("data", artist);
        Map<String, Object> indexMap = webIndexService.getPageIndexData();
        model.addAttribute("category", indexMap.get("category"));
        model.addAttribute("menu", indexMap.get("menu"));
        model.addAttribute("footer", indexMap.get("footer"));
        return "artist/detail";
    }


    /**
     * \
     *
     * @param model
     * @param params
     * @param i18n
     * @return
     */
    @RequestMapping("/search")
    public String artistListSearch(Model model, @RequestParam Map<String, String> params, @CookieValue(value = "org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE", defaultValue = "zh-CN") String i18n) {
        System.out.println("++++++++++++ ArtistController:artistListPy");

        JSONObject list = webArtService.artistList(params);
        model.addAttribute("data", list);

        Map<String, Object> indexMap = webIndexService.getPageIndexData();
        model.addAttribute("exArtistBanner", indexMap.get("exArtistBanner"));
        model.addAttribute("friendLink", indexMap.get("friendLink"));
        model.addAttribute("copyright", indexMap.get("copyright"));
        Map<String, Object> menusMap = webArtService.staticPageTree("top");
        model.addAttribute("menu", menusMap);
        model.addAttribute("i18n", i18n);

        Map<String, Object> footer = webArtService.staticPageTree("bottom");
        model.addAttribute("footer", footer);
        return "artist/search";
    }

}
